package com.systop.busafter.journalism.controller;

import com.systop.busafter.journalism.service.JsmService;
import com.systop.commer.Io;
import com.systop.commer.ResponseResult;
import com.systop.commer.pojo.Journalism;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.List;

@RestController
public class JsmController {
    @Autowired
    private JsmService journalismService;
    //通过id修改
    @RequestMapping(value="/updatajournalism",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult updatajournalism(HttpServletRequest request, @RequestParam(value = "journalism_content1",required = false) MultipartFile journalism_content1, @RequestParam(value = "journalism_id")Integer journalism_id, @RequestParam(value = "journalism_title")String journalism_title, @RequestParam(value = "journalism_content2")String journalism_content2, @RequestParam(value = "journalism_content3")String journalism_content3, @RequestParam(value = "journalism_state")int journalism_state, @RequestParam(value = "journalism_remarks")String journalism_remarks, @RequestParam(value = "journalism_s")String journalism_s) throws IOException {
        Journalism journalism = new Journalism();
        journalism.setJournalism_content1(Io.img(journalism_content1, request));
        journalism.setJournalism_title(journalism_title);
        journalism.setJournalism_content2(journalism_content2);
        journalism.setJournalism_content3(journalism_content3);
        journalism.setJournalism_state(journalism_state);
        journalism.setJournalism_remarks(journalism_remarks);
        journalism.setJournalism_s(journalism_s);
        journalism.setJournalism_id(journalism_id);
        int i =  journalismService.updatajournalism(journalism);
        if (i == 0) {
            return ResponseResult.build(500, "修改失败");
        } else {
            return ResponseResult.build(200, "修改成功", i);
        }
    }
    //添加
    @RequestMapping(value="/insertjournalism",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult insertjournalism( HttpServletRequest request,@RequestParam(value = "journalism_content1",required = false) MultipartFile journalism_content1, @RequestParam(value = "journalism_title")String journalism_title, @RequestParam(value = "journalism_content2")String journalism_content2, @RequestParam(value = "journalism_content3")String journalism_content3, @RequestParam(value = "journalism_state")int journalism_state, @RequestParam(value = "journalism_remarks")String journalism_remarks, @RequestParam(value = "journalism_s")String journalism_s) throws IOException {
        Journalism journalism = new Journalism();
        journalism.setJournalism_content1(Io.img(journalism_content1,request));
        journalism.setJournalism_title(journalism_title);
        journalism.setJournalism_content2(journalism_content2);
        journalism.setJournalism_content3(journalism_content3);
        journalism.setJournalism_state(journalism_state);
        journalism.setJournalism_remarks(journalism_remarks);
        journalism.setJournalism_s(journalism_s);
        int i =  journalismService.insertjournalism(journalism);
        if (i == 0) {
            return ResponseResult.build(500, "添加失败");
        } else {
            return ResponseResult.build(200, "添加成功", i);
        }
    }

    //查看全部
    @RequestMapping(value="/journalismListall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult journalismListall(){
        List<Journalism> list =  journalismService.journalismListall();
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过id查
    @RequestMapping(value="/journalismIdlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult journalismIdlist(@RequestBody Journalism journalism){
        List<Journalism> list =  journalismService.journalismIdlist(journalism.getJournalism_id());
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //删除
    @RequestMapping(value="/deletejournalism",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult deletejournalism(@RequestBody Journalism journalism){
       int i =  journalismService.deletejournalism( journalism.getJournalism_id());
        if (i == 0) {
            return ResponseResult.build(500, "删除失败");
        } else {
            return ResponseResult.build(200, "删除成功", i);
        }
    }
    //通过状态查
    @RequestMapping(value="/journalismstatelist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult journalismstatelist(@RequestBody Journalism journalism){

        List<Journalism> list =  journalismService.journalismstatelist(journalism.getJournalism_state());
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过状态查
    @RequestMapping(value="/journalismstate",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult journalismstate(){
        int  journalism_state=1;
        List<Journalism> list =  journalismService.journalismstate(journalism_state);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过id修改状态
    @RequestMapping(value="/updatajournastate",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult updatajournastate(@RequestBody Journalism journalism){
        int i =  journalismService.updatajournastate(journalism);
        if (i == 0) {
            return ResponseResult.build(500, "修改失败");
        } else {
            return ResponseResult.build(200, "修改成功", i);
        }
    }
//    //通过id修改状态（使用）
//    @RequestMapping(value="/journalismtuerT",method= RequestMethod.POST)
//    public @ResponseBody
//    ResponseResult journalismtuerT(@PathParam(value = "journalism_id") int journalism_id){
//        List<Journalism>  list = journalismService.journalismIdlist(journalism_id);
//
//        int i =  journalismService.updatajournastate(journalism);
//        if (i == 0) {
//            return ResponseResult.build(500, "修改失败");
//        } else {
//            return ResponseResult.build(200, "修改成功", i);
//        }
//    }

}
